package com.cuitrip.business.setting;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.utils.Utils;

import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created on 7/16.
 */
public class RelationActivity extends CustomUiFragmentActivity {

    @Bind(R.id.ct_wechat)
    ItemLayout mWechatView;

    @Bind(R.id.contact_phone_num)
    ItemLayout mPhoneView;

    @Bind(R.id.ct_mail)
    ItemLayout mMailView;

    @Bind(R.id.ct_facebook)
    ItemLayout mFacebookView;


    Dialog dialog;

    public static void start(Context context) {
        context.startActivity(new Intent(context, RelationActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.ct_relation);
    }

    public void initView() {
        ButterKnife.bind(this);
        mWechatView.setleftText(Html.fromHtml("微信公众号：  <font color='"+ ResourceUtil.getThemeColor()+"'red'>cuitrip</font>"));
        dialog=new Dialog(RelationActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.setContentView(R.layout.page_image);
        initImageView();
    }

    @Override
    public void initListener() {
        boolean show= I118Util.getLocale().equals(Locale.CHINA);
        mPhoneView.setVisibility(show?View.VISIBLE:View.GONE);
        mWechatView.setVisibility(show?View.VISIBLE:View.GONE);
        mFacebookView.setVisibility(!show?View.VISIBLE:View.GONE);
        mPhoneView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.requestDial(RelationActivity.this, "+8615869046600");
            }
        });
        mMailView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendEmail();
            }
        });

        mWechatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null){
                    dialog.show();
                }
            }
        });
        mFacebookView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.startBrowser(RelationActivity.this,AboutActivity.urlFacebook);
            }
        });
    }

    private void initImageView(){
        ImageView image=(ImageView)dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.we_public_num);
        image.setOnClickListener(new ImageView.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(dialog!=null){
                    dialog.dismiss();
                }
            }
        });
    }

    public void sendEmail() {
        Intent data = new Intent(Intent.ACTION_SENDTO);
        data.setData(Uri.parse("mailto:service@cuitrip.com"));
        startActivity(data);
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.setting_contact);
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }
}
